//
//  MoviesVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/14.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MoviesVC.h"
#import "iCarousel.h"
#import "Movie.h"
#import "UIImageView+WebCache.h"
#import "VariableHeader.h"
#import "MovieDetailVC.h"
@interface MoviesVC ()<iCarouselDataSource,iCarouselDelegate>
@property(nonatomic,strong) iCarousel *icarousel;
@property (weak, nonatomic) IBOutlet UILabel *movieTitleLB;
@property (weak, nonatomic) IBOutlet UILabel *movieTimeLB;
@property (weak, nonatomic) IBOutlet UILabel *appNumLB;
@property (weak, nonatomic) IBOutlet UILabel *movieScoreLB;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@property(nonatomic,strong) NSMutableArray *movies;

@property(nonatomic,strong) UIImageView *imgV;

@property(nonatomic,strong)Movie *movie;
@end

@implementation MoviesVC

- (void)initInfo
{
    [super initInfo];

    [self initView];
    
    self.navigationController.navigationBarHidden = YES;
}

-(void)initView
{
    _icarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height - [UIScreen mainScreen].bounds.size.width / 5 - [UIScreen mainScreen].bounds.size.width / 3)];
    _icarousel.delegate = self;
    _icarousel.dataSource = self;
    _icarousel.type = iCarouselTypeCoverFlow2;
    [self.view addSubview:_icarousel];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.backgroundImageView.frame.size.width, self.backgroundImageView.frame.size.height)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [self.backgroundImageView insertSubview:toolbar belowSubview:self.movieTitleLB];
    
}

-(NSMutableArray *)movies{
    if (!_movies) {
        _movies = [NSMutableArray array];
    }
    return _movies;
}


#pragma mark - iCarouselDataSource,iCarouselDelegate

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
     Movie *m =  [self.movies objectAtIndex:index];
     self.movie = m;
    [self performSegueWithIdentifier:@"idfMovieDetailVC" sender:self];
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return 10;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    
    UIImageView *img = [[UIImageView alloc] init];

    img.frame = CGRectMake(70, 40, [UIScreen mainScreen].bounds.size.width / 2, ([UIScreen mainScreen].bounds.size.width * 260) / (2 * 180));

    return img;
}
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel{
    if(!_movies){
        __weak typeof(self) weakSelf = self;
        [SVProgressHUD show];
        [TDNetwork requestMoviesPOST:movieListIP andPage:1 andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
            if (succeed) {
                [SVProgressHUD dismiss];
                NSArray *movieList = [[dictionary valueForKey:@"res"]  valueForKey:@"filmList"];
                
                for (NSDictionary *movieDic in movieList) {
                    Movie *m = [[Movie alloc]init];
                    m.moiveImage = [movieDic valueForKey:@"moiveImage"];
                    m.moiveTitle = [movieDic valueForKey:@"moiveTitle"];
                    m.releaseTime = [NSString stringWithFormat:@"上映时间：%@",[movieDic valueForKey:@"releaseTime"]];
                    m.appNum = [NSString stringWithFormat:@"已发布的影约：%@",[movieDic valueForKey:@"appNum"]];
                    m.moiveScore =  [[NSString stringWithFormat:@"网友评分：%@",  [movieDic valueForKey:@"moiveScore"]] substringToIndex:8];
                    m.appId =  [NSString stringWithFormat:@"%@", [movieDic valueForKey:@"appId"]];
 
                    m.introduction = [NSString stringWithFormat:@"影片简介：%@",[movieDic valueForKey:@"introduction"]];
                    m.appointmentCount = [NSString stringWithFormat:@"影约数：%@",[movieDic valueForKey:@"appointmentCount"]];
                    m.moiveTime = [NSString stringWithFormat:@"片长:%@",[movieDic valueForKey:@"moiveTime"]];
                    m.moiveType = [NSString stringWithFormat:@"类型:%@",[movieDic valueForKey:@"moiveType"]];
                    
                    m.movieId = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"movieId"]];
                    [weakSelf.movies addObject:m];
                }
                Movie *firstM = [weakSelf.movies firstObject];
                [weakSelf setCarouselViewMessage:firstM andCarouser:carousel];
            } else {
                [SVProgressHUD showErrorWithStatus:@"请求错误"];
            }
        }];
        
    }
    
}

-(void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel{
    
    Movie *m =  [self.movies objectAtIndex:carousel.currentItemIndex];
    [self setCarouselViewMessage:m andCarouser:carousel];

}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel {
    return 0;
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel {
    return 50;
}

//这个是设置图片间的间隔
- (CGFloat)carouselItemWidth:(iCarousel *)carousel {
    return ([UIScreen mainScreen].bounds.size.width * 4) / 5;
}

- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset
{
    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);
    
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = self.icarousel.perspective;
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * _carousel.itemWidth);
}

-(void) setCarouselViewMessage:(Movie *)m  andCarouser:(iCarousel *)carousel{

     [(UIImageView *)carousel.currentItemView sd_setImageWithURL:[NSURL URLWithString:m.moiveImage]];
    [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:m.moiveImage]];
    if (m.moiveScore.length >5) {
        [m.moiveScore substringToIndex:3];
    }
    self.movieTitleLB.text =  m.moiveTitle;
    self.movieTimeLB.text = m.releaseTime;
    self.appNumLB.text =  m.appNum;
    self.movieScoreLB.text = m.moiveScore;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    if ([segue.identifier isEqualToString:@"idfMovieDetailVC"]) {
        MovieDetailVC *movieDetailVC = segue.destinationViewController;
        
        movieDetailVC.movie = self.movie;
    }    
}


@end
